<?php
namespace app\admin\controller;
use app\common\controller\AdminBase;
use app\common\model\DictData;
use app\common\model\Dict as Model;

/**
 * 字典管理
 */
class Dict extends AdminBase
{
    /**
     * 字典分类
     */
    public function index()
    {
        $lists = Model::order('id asc')->select();
        $this->result($lists);
    }

    /**
     * 字典列表
     */
    public function get_list($keywords='',$dictId='')
    {
        $model = new DictData();
        if(!empty($keywords)){
            $model = $model->where('name','like','%'.$keywords.'%');
        }
        $lists = $model->where('dict_id',$dictId)->order('id asc')->paginate();
        $this->result(compact('lists'));
    }

    /**
     * 编辑字典
     */
    public function edit()
    {
        if ($this->request->isPost()) {
            $param = $this->request->param();
            $model = new DictData();
            if(!empty($param['id'])){
                unset($param['create_time']);
                $model = DictData::find($param['id']);
            }
            $result = $model->save($param);
            $this->result([],0,'操作成功');
        }
    }

    /**
     * 删除字典
     */
    public function del()
    {
        if ($this->request->isPost()) {
            $param = $this->request->param();
            DictData::destroy($param['id']);
            $this->result([],0,'删除成功');
        }
    }

    /**
     * 批量删除
     */
    public function batch()
    {
        if ($this->request->isPost()) {
            $param = $this->request->param();
            DictData::destroy($param['data']);
            $this->result('',0,"删除成功");
        }
    }

}
